package co.fitstart.admin.web.controller.report;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.admin.entity.report.ReportPrepare;
import co.fitstart.admin.entity.report.StudentCourseRoundStat;
import co.fitstart.admin.entity.report.StudentUnitPrice;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReportService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 平均客单价
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/student-unit-price-reports")
@MenuSetting("menu-report")
public class StudentUnitPriceReportController {
    @Autowired
    private SiteService siteService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private PrincipalService principalService;
    
    /**
     * 
     * @param login
     * @param siteId
     * @param date
     * @param period
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-student-unit-price-report")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(name = "site", required = false) Long siteId,
                             @RequestParam(name = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                             @RequestParam(name = "period", required = false, defaultValue = "day") String period) {
        ModelAndView modelAndView = new ModelAndView("report/student-unit-price-report-list");
        
        Site site = null;
        if(!login.hasSite() && siteId != null) {
            site = siteService.load(siteId);
        }else if (login.hasSite()){
            site = login.getSite();
        }
        
        if(date == null) {
            date = new Date();
        }
        
        if(!"week".equals(period) && !"month".equals(period)) {
            period = "day";
        }
        
        List<StudentUnitPrice> unitPriceList = new ArrayList<>();
        StudentUnitPrice unitPrice = null;
        List<CourseRound> courseRoundList = new ArrayList<>();
        List<StudentCourseRoundStat> roundStatList = new ArrayList<>();
        
        if(site == null && (login.isAreaManager() || login.isAreaServer())) {
            Principal principal = principalService.load(Principal.class, login.getId());
            List<Site> sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                unitPriceList = reportService.listStudentUnitPrice(sites, date, period);
                unitPrice = reportService.getStudentUnitPrice(sites, date, period);
                courseRoundList = reportService.listCourseRound(sites, date, period);
                roundStatList = reportService.listStudentCourseRoundStat(sites, date, period);
            }
        } else {
            unitPriceList = reportService.listStudentUnitPrice(site, date, period);
            if(site == null) {
                unitPrice = reportService.getStudentUnitPrice(date, period);
            }
            courseRoundList = reportService.listCourseRound(site, date, period);
            roundStatList = reportService.listStudentCourseRoundStat(site, date, period);
        }
        modelAndView.addObject("unitPriceList", CollectionUtils.constructMapValueFromList(unitPriceList, "siteName"));
        modelAndView.addObject("unitPrice", unitPrice);
        modelAndView.addObject("courseRoundList", courseRoundList);
        modelAndView.addObject("roundStatList", roundStatList);
        
        if(site != null) {
            modelAndView.addObject("selectSite", site);
        }
        modelAndView.addObject("currentDate", date);
        ReportPrepare reportPrepare = new ReportPrepare(site, date, period);
        modelAndView.addAllObjects(reportPrepare.describeReport());
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("report/student-unit-price-report-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param beginDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                         @RequestParam(value = "site", required=false) Long siteId,
                                         @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                         @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("student-unit-price-report.xls"));
        
        Site site = null;
        if(login.hasSite()) {
            site = login.getSite();
        } else if(siteId != null) {
            site = siteService.load(siteId);
        }
        
        if(beginDate == null || endDate == null) {
            beginDate = new Date();
            endDate = new Date();
        }
        
        List<Site> sites = new ArrayList<>();
        List<StudentUnitPrice> unitPriceList = new ArrayList<>();
        StudentUnitPrice unitPrice = null;
        List<CourseRound> courseRoundList = new ArrayList<>();
        List<StudentCourseRoundStat> roundStatList = new ArrayList<>();
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(siteId == null) {
                    unitPriceList = reportService.listStudentUnitPrice(sites, beginDate, endDate);
                    unitPrice = reportService.getStudentUnitPrice(sites, beginDate, endDate);
                    courseRoundList = reportService.listCourseRound(sites, beginDate, endDate);
                    roundStatList = reportService.listStudentCourseRoundStat(sites, beginDate, endDate);
                } else if(site != null && sites.contains(site)) {
                    unitPriceList = reportService.listStudentUnitPrice(site, beginDate, endDate);
                    courseRoundList = reportService.listCourseRound(site, beginDate, endDate);
                    roundStatList = reportService.listStudentCourseRoundStat(site, beginDate, endDate);
                }
            }
        } else {
            if(site == null) {
                sites = siteService.loadAll();
                unitPrice = reportService.getStudentUnitPrice(beginDate, endDate);
            }
            unitPriceList = reportService.listStudentUnitPrice(site, beginDate, endDate);
            courseRoundList = reportService.listCourseRound(site, beginDate, endDate);
            roundStatList = reportService.listStudentCourseRoundStat(site, beginDate, endDate);
        }
        
        
        if(site == null) {
            modelAndView.addObject("unitPrice", unitPrice);
        } else {
            sites.add(site);
        }
        modelAndView.addObject("courseRoundList", courseRoundList);
        modelAndView.addObject("roundStatList", roundStatList);
        modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
        modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
        modelAndView.addObject("sites", sites);
        modelAndView.addObject("unitPriceList", CollectionUtils.constructMapValueFromList(unitPriceList, "siteName"));
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        NumberFormat decFmt = new DecimalFormat("0.00");
        modelAndView.addObject("decFmt", decFmt);
        NumberFormat numFmt = new DecimalFormat("0");
        modelAndView.addObject("numFmt", numFmt);
        
        return modelAndView;
    }
}
